package com.epfresh.activity;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.fragment.MarketGoodsFragment;
import com.epfresh.fragment.MarketMerchantFragment;
import com.epfresh.global.BaseActivity;
import com.epfresh.global.BaseFragment;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GoodsAndMerchantActivity extends BaseActivity {
    private ViewPager vp_goods_merchant;
    private ImageView img_back, img_search;
    private TabLayout tl_title;

    private MarketGoodsFragment goodsFragment;
    private MarketMerchantFragment merchantFragment;
    private String[] tabTitles = {"商品", "商户"};
    private String marketId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_and_merchant);

        initIntent();
        initView();
        initVp();
    }

    @Override
    protected View getToolBar() {
        return null;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        boolean refresh = intent.getBooleanExtra("refresh", false);
        if (refresh) {
            if (merchantFragment != null) {
                merchantFragment.refresh();
            }
        }
    }

    private void initIntent() {
        marketId = getIntent().getStringExtra("marketId");
    }

    private void initView() {
        vp_goods_merchant = findViewById(R.id.vp_goods_merchant);
        img_back = findViewById(R.id.img_back);
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        img_search = findViewById(R.id.img_search);
        img_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GoodsAndMerchantActivity.this,SearchActivity.class);
                intent.putExtra("marketId",marketId);
                startActivity(intent);
            }
        });
        tl_title = findViewById(R.id.tl_title);
    }

    /**
     * 绑定TabLayout和ViewPager
     */
    private void initVp() {
        vp_goods_merchant.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), getFragmentList(), Arrays.asList(tabTitles)));
        vp_goods_merchant.setOffscreenPageLimit(2);
        tl_title.setupWithViewPager(vp_goods_merchant);
        setIndicatorWidth(tl_title, 30);
    }


    private List<BaseFragment> getFragmentList() {
        List<BaseFragment> fragmentList = new ArrayList<>();
        goodsFragment = new MarketGoodsFragment();
        merchantFragment = new MarketMerchantFragment();
        fragmentList.add(goodsFragment);
        fragmentList.add(merchantFragment);

        Bundle bundle = new Bundle();
        bundle.putString("marketId",marketId);
        goodsFragment.setArguments(bundle);
        merchantFragment.setArguments(bundle);

        return fragmentList;
    }

    class MyFragmentPagerAdapter extends FragmentPagerAdapter {
        private List<BaseFragment> fragmentList;
        private List<String> tabTitles;

        MyFragmentPagerAdapter(FragmentManager fm, List<BaseFragment> fragmentList, List<String> tabTitles) {
            super(fm);
            this.fragmentList = fragmentList;
            this.tabTitles = tabTitles;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList == null ? null : fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabTitles.get(position);
        }
    }



    /**
     * 设置tab指示器的长度(随tab的长度)
     *
     * @param tabLayout
     * @param dpTabMargin
     */
    public void setIndicatorWidth(final TabLayout tabLayout, final int dpTabMargin) {
        tabLayout.post(new Runnable() {

            @Override
            public void run() {
                try {
                    LinearLayout mTabStrip = (LinearLayout) tabLayout.getChildAt(0);
                    int dpMargin = LocalDisplay.dp2px(dpTabMargin);
                    for (int i = 0; i < mTabStrip.getChildCount(); i++) {
                        View tabView = mTabStrip.getChildAt(i);
                        Field mTextViewField = tabView.getClass().getDeclaredField("mTextView");
                        mTextViewField.setAccessible(true);
                        TextView mTextView = (TextView) mTextViewField.get(tabView);
                        mTextView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                        tabView.setPadding(0, 0, 0, 0);
                        int width;
                        width = mTextView.getWidth();
                        if (width == 0) {
                            mTextView.measure(0, 0);
                            width = mTextView.getMeasuredWidth();
                        }
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
                        params.width = width;
                        params.leftMargin = dpMargin;
                        params.rightMargin = dpMargin;
                        tabView.setLayoutParams(params);
                        tabView.invalidate();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
    }
}
